<?php

/**
 * hipermedio actions.
 *
 * @package    hipermedio
 * @subpackage hipermedio
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class hipermedioActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->pager = new sfDoctrinePager(
      'Hipermedio', 20
    );
    
    $this->hipermedios = Doctrine_Core::getTable('Hipermedio')
      ->createQuery('a');
    
    if(!$this->getUser()->hasCredential('profesor')){
        $this->hipermedios->where('usuario = ?', $this->getUser()->getGuardUser()->getId());
    }
    
    $this->pager->setQuery($this->hipermedios);
    $this->pager->setPage($request->getParameter('page', 1));
    $this->pager->init();
    

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->hipermedio = Doctrine_Core::getTable('Hipermedio')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->hipermedio);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new HipermedioForm();
    $ws = $this->form->getWidgetSchema();
    $ws['usuario']->setAttribute('value', $this->getUser()->getGuardUser()->getId());
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new HipermedioForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($hipermedio = Doctrine_Core::getTable('Hipermedio')->find(array($request->getParameter('id'))), sprintf('Object hipermedio does not exist (%s).', $request->getParameter('id')));
    $this->form = new HipermedioForm($hipermedio);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($hipermedio = Doctrine_Core::getTable('Hipermedio')->find(array($request->getParameter('id'))), sprintf('Object hipermedio does not exist (%s).', $request->getParameter('id')));
    $this->form = new HipermedioForm($hipermedio);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();
    
    $this->forward404Unless($hipermedio = Doctrine_Core::getTable('Hipermedio')->find(array($request->getParameter('id'))), sprintf('Object hipermedio does not exist (%s).', $request->getParameter('id')));
    
    // Verificamos si es el dueño
    if($this->getUser()->getGuardUser()->getId() == $hipermedio->getUsuario() ){
        $hipermedio->delete();
    }
    // Si no es el mismo usuario, entonces redirigmos a la página de error
    else{
        $this->redirect('mapa/error');
    }
    
    $this->redirect('hipermedio/index');
  }
  
  public function executeTutorial(sfWebRequest $request)
  {
    $this->forward404Unless($hipermedio = Doctrine_Core::getTable('Hipermedio')->find(array($request->getParameter('id'))), sprintf('Object hipermedio does not exist (%s).', $request->getParameter('id')));
    $hipermedio->setTutorial(true);
    $hipermedio->save();
    
    return $this->renderText('success');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $hipermedio = $form->save();

      $this->redirect('ensayo/new?hipermedio_id='.$hipermedio->getId());
    }
  }
}
